package com.dhcc.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.entity.SysDept;
import com.dhcc.entity.SysDeptPosition;
import com.dhcc.entity.SysPosition;
import com.dhcc.mapper.SysDeptPositionMapper;
import com.dhcc.service.SysDeptPositionService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 机构（部门）-岗位关联 接口实现类
 * @ProjectName Dhcc-Cloud
 * @PackageName com.dhcc.service.impl
 * @Title SysDeptPositionServiceImpl
 * @Date 2024/12/5 下午3:41
 * @Author LiuGuoting
 */
@Service
public class SysDeptPositionServiceImpl extends ServiceImpl<SysDeptPositionMapper, SysDeptPosition> implements SysDeptPositionService {

    @Override
    public List<SysDeptPosition> getSysDeptPositionList(Wrapper<SysDeptPosition> queryWrapper) {
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 获取部门-岗位树结构
     *
     * @param departments 部门列表
     * @param positions   岗位列表
     * @return 部门-岗位树结构，节点包含子部门和岗位信息
     */
    @Override
    public List<Map<String, Object>> getDeptPositionTree(Set<SysDept> departments, Set<SysPosition> positions, Set<SysDeptPosition> deptPositions) {
        // 1. 将部门和岗位转换为Map，方便快速查找
        Map<String, SysDept> deptMap = new HashMap<>();
        for (SysDept dept : departments) {
            deptMap.put(dept.getId(), dept);
        }
        Map<String, SysPosition> positionMap = new HashMap<>();
        for (SysPosition position : positions) {
            positionMap.put(position.getId(), position);
        }
        // 2. 构建部门-岗位的映射关系
        Map<String, List<SysPosition>> deptPositionMap = new HashMap<>();
        for (SysDeptPosition deptPosition : deptPositions) {
            String deptId = deptPosition.getDepartmentId().toString();
            String positionId = deptPosition.getPositionId().toString();

            // 获取岗位信息
            SysPosition position = positionMap.get(positionId);
            if (position != null) {
                deptPositionMap.computeIfAbsent(deptId, k -> new ArrayList<>()).add(position);
            }
        }

        // 3. 构建部门树结构
        List<Map<String, Object>> result = new ArrayList<>();
        for (SysDept dept : departments) {
            // 只处理根部门（fatherDepartmentId为null）
            if (dept.getFatherDepartmentId() == null) {
                // 构建部门节点
                Map<String, Object> deptNode = buildDeptNode(dept, deptMap, deptPositionMap);
                result.add(deptNode);
            }
        }

        return result;
    }

    /**
     * 构建部门节点
     *
     * @param dept           当前部门
     * @param deptMap        部门Map
     * @param deptPositionMap 部门-岗位映射
     * @return 部门节点，包含子部门和岗位信息
     */
    private Map<String, Object> buildDeptNode(SysDept dept, Map<String, SysDept> deptMap, Map<String, List<SysPosition>> deptPositionMap) {
        Map<String, Object> deptNode = new HashMap<>();
        deptNode.put("id", dept.getId());
        deptNode.put("departmentName", dept.getDepartmentName());
        deptNode.put("departmentType", dept.getDepartmentType());
        deptNode.put("description", dept.getDescription());
        deptNode.put("sort", dept.getSort());

        // 添加当前部门的岗位信息
        List<SysPosition> deptPositionsList = deptPositionMap.getOrDefault(dept.getId(), new ArrayList<>());
        deptNode.put("positions", deptPositionsList);

        // 递归查找并添加子部门
        List<Map<String, Object>> children = new ArrayList<>();
        for (SysDept childDept : deptMap.values()) {
            if (dept.getId().equals(childDept.getFatherDepartmentId())) {
                Map<String, Object> childNode = buildDeptNode(childDept, deptMap, deptPositionMap);
                children.add(childNode);
            }
        }
        deptNode.put("children", children);

        return deptNode;
    }
}
